﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;

namespace WPDataBinding
{
    public partial class ProductListJSONPage : PhoneApplicationPage
    {
        public ProductListJSONPage()
        {
            InitializeComponent();

            WebClient web = new WebClient();
            web.DownloadStringAsync(new Uri("http://product77.azurewebsites.net/ProductJSON.ashx",UriKind.Absolute));
            web.DownloadStringCompleted += web_DownloadStringCompleted;
        }

        void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //List<ProductService.Product> p = new List<ProductService.Product>();
            string json =  e.Result.ToString();
            RootObject pList = JsonConvert.DeserializeObject<RootObject>(json);
            productListBox.ItemsSource = pList.ProductList;
        
        }
    }

    public class ProductList
    {
        public int ID { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
        public string ProductImage { get; set; }
        public int CategoryID { get; set; }
    }

    public class RootObject
    {
        public List<ProductList> ProductList { get; set; }
    }
}